<?php
namespace app\modules\api\controllers;

use yii;
use app\models\Member;
use app\models\Star;
use app\models\DealLog;
use app\models\Goods;
use app\models\GoodsSearch;
use ac\component\tool\Tool;
use ac\component\exception\ErrMap;
use app\models\OperationLog;

class MemberController extends AuthController
{

    /*用户信息*/
    public function actionInfo()
    {
        $info = Member::find()->select(['account', 'nickname', 'headimgurl', 'add_time'])
                      ->where('member_id = :id', [':id' => $this->userInfo->member_id])
                      ->asArray()->one();
        if(!is_null($info)){
        // P($info);
            $info['account'] = $info['account']/100;
            return Tool::showRes([0, '获取成功'], $info);
        }
        return Tool::showRes([0, '获取成功'], array());
    }

    /*流水日志*/
    public function actionDealLog() {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();
        $count = DealLog::find()->where('member_id = :id and pay_status = 2', [':id' => $this->userInfo->member_id])->count();
        $dealLogList = DealLog::find()->select(['money', 'balance', 'pay_status', 'way', 'remark', 'add_time'])
                            ->with("goods")
                            ->where('member_id = :id and pay_status = 2', [':id' => $this->userInfo->member_id])
                            ->offset($pageSize*($currPage-1))->limit($pageSize)
                            ->orderBy(['add_time' => SORT_DESC])
                            ->asArray()->all();
        foreach($dealLogList as $k => $v) {
            $dealLogList[$k]["money"] = $v["money"] / 100;
        }
        // P($dealLogList);
        $res = array(
            'list' => $dealLogList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*收藏/取消检品*/
    public function actionStarGoods() {
        if(Yii::$app->request->isPost and isset($this->post["goods_id"])){
            $this->userInfo = Yii::$app->session->get('loginInfo');
            $mid = $this->userInfo->member_id;

            $star = Star::find()
                    ->where('goods_id = :id and member_id = :mid', [':id' => $this->post["goods_id"], ':mid' => $mid])
                    ->one();
            // P($star);
            if(!is_null($star)) {
                $star->delete();
                $backInfo = [0, '取消收藏成功'];
            } else {
                $this->post['member_id'] = $mid;
                $data = [
                    "Star" => $this->post
                ];
                // P($data);
                $star = new Star;
                if($star->addStar($data)){
                    $backInfo = [0, '添加收藏成功'];//返回信息
                }else{
                    if($star->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                    }
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*我的收藏*/
    public function actionStarList() {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();
        $count = Star::find()->where('member_id = :id', [':id' => $this->userInfo->member_id])->count();
        $starList = Star::find()->with("goods")
                            ->where('member_id = :id', [':id' => $this->userInfo->member_id])
                            ->offset($pageSize*($currPage-1))->limit($pageSize)
                            ->orderBy(['add_time' => SORT_DESC])
                            ->asArray()->all();
        // P($starList);
        $res = array(
            'list' => $starList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*我的备案(成功/失败)*/
    public function actionMyBei() {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        if(isset($this->get['pagesize'])){
            $pageSize = intval($this->get['pagesize']?$this->get['pagesize']:Tool::getPageSize());
        }else{
            $pageSize = Tool::getPageSize();
        }
        // P($this->get);
        if(!isset($this->get['state'])) {
            $this->get['state'] = 1;
        }
        // P($this->get['state']);
        $search = (new GoodsSearch())->search($this->get);
        if($search === false){//验证搜索参数不通过
            $count = 0;
            $goodsList = array();
        }else{
            $count = $search->andWhere('member_id = :id', [':id' => $this->userInfo->member_id])->count();
            $goodsList = $search->andWhere('member_id = :id', [':id' => $this->userInfo->member_id])
                          ->offset($pageSize*($currPage-1))->limit($pageSize)
                          ->orderBy([
                                'add_time' => SORT_DESC
                            ])
                          // ->createCommand()->getRawSql();
                          ->asArray()->all();
        }
        // P($goodsList);
        $res = array(
            'list' => $goodsList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

}
